package com.easthome.common.system;

import com.easthome.model.system.SysMenu;
import com.easthome.vo.system.MetaVo;
import com.easthome.vo.system.RouterVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @description:
 * @author: 朱敦宇
 * @time: 2023/11/9 11:28
 */
public class MenuHelper {
    public static List<SysMenu> toTreeMenu(List<SysMenu> sysMenus) {
        ArrayList<SysMenu> treeMenu = new ArrayList<>();
        //1.找到一级目录
        for (SysMenu sysMenu : sysMenus) {
            //因为是包装Long类所以进行拆箱
            if (sysMenu.getParentId().longValue()==0){
                treeMenu.add(findNext(sysMenu,sysMenus));
            }
        }
        return treeMenu;
    }

    private static SysMenu findNext(SysMenu sysMenu, List<SysMenu> sysMenus) {
        if(sysMenu.getChildren() == null){
            sysMenu.setChildren(new ArrayList<SysMenu>());
        }
        //找子目录
        for (SysMenu menu : sysMenus) {
            if(menu.getParentId().longValue() == sysMenu.getId()){
                sysMenu.getChildren().add(findNext(menu,sysMenus));
            }
        }

        return sysMenu;
    }

    public static List<RouterVo> toRouterTree(List<SysMenu> menuList) {
        ArrayList<RouterVo> routerVoList = new ArrayList<>();
        for (SysMenu sysMenu : menuList) {
            RouterVo routerVo = new RouterVo();
            //1.先设为false，后边判断是否有子菜单，有设为true
            routerVo.setAlwaysShow(false);
            //2.是否隐藏
            routerVo.setHidden(false);
            //3.设置路径
            routerVo.setPath(getPath(sysMenu));
            //4
            routerVo.setComponent(sysMenu.getComponent());
            //5
            routerVo.setMeta(new MetaVo(sysMenu.getName(),sysMenu.getIcon()));
            //6.设置子路由

            if(sysMenu.getType() == 1){
                List<SysMenu> collect = sysMenu.getChildren().stream().filter(menu -> !StringUtils.isEmpty(menu.getPath())).collect(Collectors.toList());
                for (SysMenu menu : collect) {
                    RouterVo routerVo2 = new RouterVo();
                    //1.先设为false，后边判断是否有子菜单，有设为true
                    routerVo2.setAlwaysShow(false);
                    //2.是否隐藏
                    routerVo2.setHidden(true);
                    //3.设置路径
                    routerVo2.setPath(getPath(menu));
                    //4
                    routerVo2.setComponent(menu.getComponent());
                    //5
                    routerVo2.setMeta(new MetaVo(menu.getName(),menu.getIcon()));
                    routerVoList.add(routerVo2);
                }
            }else {
                if(!CollectionUtils.isEmpty(sysMenu.getChildren())){
                    routerVo.setAlwaysShow(true);
                }
                routerVo.setChildren(toRouterTree(sysMenu.getChildren()));
            }
            routerVoList.add(routerVo);

        }
        return routerVoList;
    }

    /**
     * @description: 获取路径
     * @param sysMenu

     */
    private static String getPath(SysMenu sysMenu) {
        String path=sysMenu.getPath();
        //如果为一级菜单  path前面加/
        if(sysMenu.getType().intValue() == 0){
            path="/"+path;
        }
        return path;
    }

}
